﻿using Assets.GameFramework.Scripts.Runtime.DailyTask;
using GameFramework.Event;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using static DG.Tweening.DOTweenAnimation;
using static UnityEngine.Rendering.DebugUI.Table;

namespace TowerDefence_Zombie.Data
{
    public sealed partial class DataPlayer
    {
        DailyTask _dailyTask => GameEntry.Data.GetData<DailyTask>();

        public void InitDailyTaskEvent()
        {
            GameEntry.Event.Subscribe(TaskRunEventArgs.EventId, SetDailyTaskData);
            GameEntry.Event.Subscribe(TaskAcrossDayeEventArgs.EventId, RefreshDailyTaskData);
        }


        public bool TryGetSingleTaskProgress(out int curDay, out int totalDay)
        {
            curDay = 0;
            totalDay = 0;
            var SingleTasks = _dailyTask.GetAllSingleTask();
            if (_playerData.SignTask.Count > SingleTasks.Count)
                return false;

            for (int i = 0; i < SingleTasks.Count; i++)
            {
                if(_playerData.SignTask.Count <= SingleTasks[i].DayNum)
                {
                    curDay = _playerData.SignTask.Count;
                    totalDay = SingleTasks[i].DayNum;
                    return true;
                }
            }
            return true;
        }

        public int GetToDaySignTaskIntegral()
        {
            var SingleTasks = _dailyTask.GetAllSingleTask();
            for (int i = 0; i < SingleTasks.Count; i++)
            {
                if (_playerData.SignTask.Count <= SingleTasks[i].DayNum)
                {
                    if (_playerData.SignTask.Count == SingleTasks[i].DayNum)
                        return 1;
                    else
                        return 0;
                }
            }
            return 0;
        }

        public void GetDailyTaskProgress(int taskId, out int completeStege, out int curProgress, out int totalProgress)
        {
            completeStege = 0;
            curProgress = 0;
            totalProgress = 1;
            int completeNum = 0;
            int[] completeTotal = null;
            if (taskId == 1)
            {
                completeNum = _playerData.PlayerTaskData.CurDayTaskSchedule[1];
                _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 1, out completeTotal);
            }
            else if (taskId == 2)
            {
                completeNum = _playerData.PlayerTaskData.CurDayTaskSchedule[2];
                _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 2, out completeTotal);
            }
            else if (taskId == 3)
            {
                completeNum = _playerData.PlayerTaskData.CurDayTaskSchedule[3];
                _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 3, out completeTotal);
            }
            else if (taskId == 4)
            {
                completeNum = _playerData.PlayerTaskData.CurDayTaskSchedule[4];
                _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 4, out completeTotal);
            }

            for (int i = 0; i < completeTotal.Length; i++)
            {
                if(completeNum >= completeTotal[i])
                    completeStege++;
                else
                {
                    curProgress   = completeNum;
                    totalProgress = completeTotal[i];
                    return;
                }
            }

            curProgress = completeTotal[^1];
            totalProgress = completeTotal[^1];
            return;
        }

        public int GetDailyTaskIntegral()
        {
            int completeNum = 0;

            int[] completeTotal = null;
            _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 1, out completeTotal);
            for (int i = 0; i < completeTotal.Length; i++)
            {
                if (_playerData.PlayerTaskData.CurDayTaskSchedule[1] >= completeTotal[i])
                    completeNum++;
            }

            _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 2, out completeTotal);
            for (int i = 0; i < completeTotal.Length; i++)
            {
                if (_playerData.PlayerTaskData.CurDayTaskSchedule[2] >= completeTotal[i])
                    completeNum++;
            }

            _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 3, out completeTotal);
            for (int i = 0; i < completeTotal.Length; i++)
            {
                if (_playerData.PlayerTaskData.CurDayTaskSchedule[3] >= completeTotal[i])
                    completeNum++;
            }

            _dailyTask.GetDailyTaskData(GetDailyTaskDatNum(), 4, out completeTotal);
            for (int i = 0; i < completeTotal.Length; i++)
            {
                if (_playerData.PlayerTaskData.CurDayTaskSchedule[4] >= completeTotal[i])
                    completeNum++;
            }

            return completeNum;

        }

        public void GetDailyTaskReward(out DailyTaskData dailyTaskData)
        {
            dailyTaskData =_dailyTask.GetDailyTaskData(GetDailyTaskDatNum());
        }

        public void GetDailyTaskReceiveId( out List<int> receiveId)
        {
            receiveId = _playerData.PlayerTaskData.ReceiveId;
        }

        public void SetDailyTaskReceiveId(int receiveId)
        {
            _playerData.PlayerTaskData.ReceiveId.Add(receiveId);
        }

        public int GetTotalTaskIntegral()
        {
            return GetDailyTaskIntegral() + GetToDaySignTaskIntegral();
        }


        private void SetDailyTaskData(object sender, GameEventArgs e)
        {
            TaskRunEventArgs taskRunArgs = (TaskRunEventArgs)e;
            if(taskRunArgs == null)
                return;
            int taskId = taskRunArgs.TaskId;
            int completeNum = taskRunArgs.CompletedCount;
            if (_playerData.PlayerTaskData.CurDayTaskSchedule.ContainsKey(taskId))
            {
                _playerData.PlayerTaskData.CurDayTaskSchedule[taskId] += completeNum;
            }
        }

        private void RefreshDailyTaskData(object sender, GameEventArgs e)
        {
            TaskAcrossDayeEventArgs taskAcrossDayeArgs = (TaskAcrossDayeEventArgs)e;
            if (taskAcrossDayeArgs == null)
                return;

           DateTime nowTime = DateTime.Now;
            DateTime CurTaskDay = DateTime.Parse(_playerData.PlayerTaskData.CurTaskDayTime);
            if((nowTime - CurTaskDay).Days >0 && nowTime.Hour >= 3)
                _playerData.PlayerTaskData.CurTaskDayTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 3, 0, 0).ToString();
            _playerData.PlayerTaskData.CurDayTaskSchedule[1] = 0;
            _playerData.PlayerTaskData.CurDayTaskSchedule[2] = 0;
            _playerData.PlayerTaskData.CurDayTaskSchedule[3] = 0;
            _playerData.PlayerTaskData.CurDayTaskSchedule[4] = 0;
        }


        public int GetDailyTaskDatNum()
        {
            DateTime FirstDay = DateTime.Parse(_playerData.PlayerTaskData.FirstTaskDayTime);
            DateTime CurTaskDay = DateTime.Parse(_playerData.PlayerTaskData.CurTaskDayTime);
            int TotalDays = (CurTaskDay - FirstDay).Days;
            return TotalDays;
        }

    }
}
